#include "CurveTableModel.h"

CurveTableModel::CurveTableModel(QObject *parent) : QAbstractTableModel(parent)
{

}

CurveTableModel::~CurveTableModel()
{
}

void CurveTableModel::SetHeaders(const QStringList headers)
{
    this->headers = headers;
}

void CurveTableModel::SetShowPoints(const QList<QSharedPointer<Point>> &showPoints)
{
    beginResetModel();
    this->showPoints = showPoints;
    endResetModel();

    Update();

}

void CurveTableModel::Update()
{
    auto t1 = index(0, 0);
    auto t2 = index(showPoints.count() - 1, headers.count() - 1);
    emit dataChanged(t1, t2);
}

int CurveTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return showPoints.count();
}

int CurveTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return headers.count();
}

QVariant CurveTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || showPoints.empty())
    {
        return QVariant();
    }

    int nRow = index.row();
    int nCol = index.column();

    auto point = showPoints.at(nRow);
    if (point == nullptr)
    {
        return QVariant();
    }
    if (Qt::DisplayRole == role || Qt::ToolTipRole == role)
    {
        switch (nCol) {
        case 0:
            return nRow;
        break;
        case 1:
            return point->GetName();
        break;
        case 2:
            return point->GetT1();
        break;
        case 3:
            return point->GetT2();
        break;
        }
    }
    else if (role ==  Qt::TextAlignmentRole)
    {
        return Qt::AlignCenter;
    }

    return QVariant();


    return QVariant();
}

QVariant CurveTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Orientation::Horizontal)
    {
        if (section < headers.size())
        {
            return headers.at(section);
        }
    }
    return QVariant();
}
